package com.apisix.service.service;

import com.apisix.service.config.ApisixConfig;
import com.apisix.service.entity.Consumer;
import com.apisix.service.util.KeyGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApisixService {

    private final RestTemplate restTemplate;
    private final ApisixConfig apisixConfig;

    @Autowired
    public ApisixService(RestTemplate restTemplate, ApisixConfig apisixConfig) {
        this.restTemplate = restTemplate;
        this.apisixConfig = apisixConfig;
    }

    private HttpHeaders createHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("X-API-KEY", apisixConfig.getApiKey());
        return headers;
    }
    
    public Map<String, Object> createConsumer(Consumer consumer) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/consumers";
        
        Map<String, Object> consumerData = new HashMap<>();
        consumerData.put("username", consumer.getAppId());
        consumerData.put("desc", consumer.getDescription());
        
        // 配置插件
        Map<String, Object> plugins = new HashMap<>();
        
        // 配置key-auth插件
        Map<String, Object> keyAuth = new HashMap<>();
        keyAuth.put("key", consumer.getAppSecret());
        plugins.put("key-auth", keyAuth);
        
        // 配置jwt-auth插件
        Map<String, Object> jwtAuth = new HashMap<>();
        jwtAuth.put("key", consumer.getAppId());
        jwtAuth.put("algorithm", "RS256");
        jwtAuth.put("public_key", consumer.getRsaPublicKey());
        jwtAuth.put("private_key", consumer.getRsaPrivateKey());
        plugins.put("jwt-auth", jwtAuth);
        
        consumerData.put("plugins", plugins);
        
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(consumerData, createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.PUT, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> deleteConsumer(String appId) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/consumers/" + appId;
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.DELETE, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> getConsumer(String appId) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/consumers/" + appId;
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.GET, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public List<Map<String, Object>> getConsumers() {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/consumers";
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.GET, 
                entity, 
                Map.class
        );
        
        Map<String, Object> body = response.getBody();
        return (List<Map<String, Object>>) body.get("list");
    }
    
    public Map<String, Object> createRoute(Map<String, Object> routeData) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/routes";
        
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(routeData, createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.POST, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> updateRoute(String routeId, Map<String, Object> routeData) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/routes/" + routeId;
        
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(routeData, createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.PUT, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> deleteRoute(String routeId) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/routes/" + routeId;
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.DELETE, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> getRoute(String routeId) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/routes/" + routeId;
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.GET, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public List<Map<String, Object>> getRoutes() {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/routes";
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.GET, 
                entity, 
                Map.class
        );
        
        Map<String, Object> body = response.getBody();
        return (List<Map<String, Object>>) body.get("list");
    }
    
    public Map<String, Object> createUpstream(Map<String, Object> upstreamData) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/upstreams";
        
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(upstreamData, createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.POST, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> updateUpstream(String upstreamId, Map<String, Object> upstreamData) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/upstreams/" + upstreamId;
        
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(upstreamData, createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.PUT, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> deleteUpstream(String upstreamId) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/upstreams/" + upstreamId;
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.DELETE, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> getUpstream(String upstreamId) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/upstreams/" + upstreamId;
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.GET, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public List<Map<String, Object>> getUpstreams() {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/upstreams";
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.GET, 
                entity, 
                Map.class
        );
        
        Map<String, Object> body = response.getBody();
        return (List<Map<String, Object>>) body.get("list");
    }
    
    public Map<String, Object> createService(Map<String, Object> serviceData) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/services";
        
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(serviceData, createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.POST, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> updateService(String serviceId, Map<String, Object> serviceData) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/services/" + serviceId;
        
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(serviceData, createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.PUT, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> deleteService(String serviceId) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/services/" + serviceId;
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.DELETE, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public Map<String, Object> getService(String serviceId) {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/services/" + serviceId;
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.GET, 
                entity, 
                Map.class
        );
        
        return response.getBody();
    }
    
    public List<Map<String, Object>> getServices() {
        String url = apisixConfig.getBaseUrl() + "/apisix/admin/services";
        
        HttpEntity<Void> entity = new HttpEntity<>(createHeaders());
        
        ResponseEntity<Map> response = restTemplate.exchange(
                url, 
                HttpMethod.GET, 
                entity, 
                Map.class
        );
        
        Map<String, Object> body = response.getBody();
        return (List<Map<String, Object>>) body.get("list");
    }
} 